from flask import request, Blueprint

from ..utils import Response, Redis, MailUtil

verification_blueprint = Blueprint('verification_blueprint', __name__, url_prefix='/verificationCode')

import random

def generate_verification_code():
    # 随机生成6位数字验证码
    code = ''.join(random.choices('0123456789', k=6))
    return code
@verification_blueprint.route('/', methods=['GET'])
def verification():  # put application's code here
    # 从 URL 查询参数中读取 email 和 codeType
    email = request.args.get('email')
    code_type = request.args.get('codeType')

    # 检查必填参数是否存在
    if not email or not code_type:
        return Response.error(505,"参数错误").to_dict()

    # 1.随机生成6位数验证码
    code = generate_verification_code()

    # 2.读入redis5分钟有效期 发送验证码
    Redis.write(email,code,60*5)

    MailUtil.send_verification_email(email,code)




    return Response.success_static(200,"发送成功").to_dict()


